package com.chendx.store.order.mq.queue;

import com.chendx.store.common.constants.ampq.AMQPConstants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * Created by chendx on 2022/4/24.
 */
@Configuration
public class QueueConfig {

    @Autowired
    private Environment env;

    @Bean
    public Exchange basicExchange(){
        return ExchangeBuilder.directExchange(env.getProperty(AMQPConstants.MQ_ORDER_EXCHANGE_ORDERTIMER)).build();
    }

    @Bean
    public Queue ordertimer(){
        return new Queue(env.getProperty(AMQPConstants.MQ_ORDER_QUEUE_ORDERTIMER),true);
    }

    @Bean
    public Queue ordertimerdelay(){
        return QueueBuilder.durable(env.getProperty(AMQPConstants.MQ_ORDER_QUEUE_ORDERTIMERDELAY))
                .withArgument(AMQPConstants.X_DEAD_LETTER_EXCHANGE,env.getProperty(AMQPConstants.MQ_ORDER_EXCHANGE_ORDERTIMER))
                .withArgument(AMQPConstants.X_DEAD_LETTER_ROUTING_KEY,env.getProperty(AMQPConstants.MQ_ORDER_ROUTING_TIMERKEY))
                .build();
    }

    @Bean
    public Binding binding(){
        return BindingBuilder.bind(ordertimer())
                .to(basicExchange())
                .with(env.getProperty(AMQPConstants.MQ_ORDER_ROUTING_TIMERKEY))
                .noargs();
    }
}
